<?php

{{ builderInfo.php|raw }}

namespace {{ thisModel.namespace }};

class {{ thisModel.class }} extends \ADIOS\Core\Model {

{% if _prototypeBuilder.hasCallbacks %}
  use \{{ thisModel.namespace }}\Callbacks\{{ thisModel.class }};
{% endif %}

{% if thisModel.config.constants is iterable %}
  {% for constName, constValue in thisModel.config.constants %}
    const {{ constName }} = {{ constValue }};
  {% endfor %}
{% endif %}

  public bool $isJunctionTable = {{ thisModel.config.isJunctionTable ? 'TRUE' : 'FALSE' }};

  public ?array $tableParams =
{{ varExport(thisModel.config.tableParams, "    ")|raw }}
  ;
  public ?array $formParams =
{{ varExport(thisModel.config.formParams, "    ")|raw }}
  ;

  public string $tableEndpoint = "{{ thisModel.config.tableEndpoint }}";
  public string $formEndpoint = "{{ thisModel.config.formEndpoint }}";

  public string $sqlName = "{{ thisModel.config.sqlName|raw }}";
  public string $urlBase = "{{ thisModel.config.urlBase|raw }}";
  public ?array $crud =
{{ varExport(thisModel.config.crud, "    ")|raw }}
  ;
  {% if thisModel.config.lookupSqlValue %}public ?string $lookupSqlValue = "{{ thisModel.config.lookupSqlValue|raw }}";{% endif %}

  public ?array $junctions =
{{ varExport(thisModel.config.junctions, "    ")|raw }}
  ;

  public bool $storeRecordInfo = {%if thisModel.config.storeRecordInfo %}TRUE{% else %}FALSE{% endif %};

  public function __construct($appOrAttributes = NULL, $eloquentQuery = NULL) {
    parent::__construct($appOrAttributes, $eloquentQuery);

    $this->tableParams = 
      {{ varExport(thisModel.config.tableParams | default(null), "    ")|raw }}
    ;
    $this->formParams = 
      {{ varExport(thisModel.config.formParams | default(null), "    ")|raw }}
    ;
  }

  public function columns(array $columns = []): array
  {
    return parent::columns([
{% for colName, colDefinition in thisModel.config.columns %}
      "{{ colName }}" =>
{{ varExport(colDefinition, "        ")|raw }}
      ,
{% endfor %}
    ]);
  }

  public function indexes($columns = []) {
    return parent::indexes([
{% for colName, colDefinition in thisModel.config.indexes %}
      "{{ colName }}" =>
{{ varExport(colDefinition, "        ")|raw }}
      ,
{% endfor %}
    ]);
  }

  public function cards($cards = []) {
    return parent::cards([
  {% for cardName, cardDefinition in thisModel.config.cards %}
      "{{ cardName }}" =>
      {{ varExport(cardDefinition, "        ")|raw }}
      ,
  {% endfor %}
    ]);
  }

  // Eloquent relations START
{% for eloquentRelationship in thisModel.config.eloquentRelationships %}
  public function {{ eloquentRelationship['column'] }}(): \Illuminate\Database\Eloquent\Relations\{{ eloquentRelationship['type'] }} {
    return $this->{{ eloquentRelationship['type'] }}(\{{ eloquentRelationship['model'] }}::class, '{{ eloquentRelationship['column'] }}');
  }

{% endfor %}

  // Eloquent relations END

{% if (thisModel.config.onFormParams) %}
  public function onFormParams(\ADIOS\Core\ViewsWithController\Form $formObject): array
  {
    $data = $formObject->data;
    $params = $formObject->params;

    $id = (int) $data["id"];

    if ($id <= 0) {
      $params["template"] =
{{ varExport(thisModel.config.formParams.templateForInserting, "        ")|raw }}
      ;
    } else {
      $params["template"] =
{{ varExport(thisModel.config.formParams.templateForEditing, "        ")|raw }}
      ;
    }

    $params["default_values"] =
{{ varExport(thisModel.config.formParams.defaultValues, "      ")|raw }}
    ;

    return $this->app->dispatchEventToPlugins("onModelAfterFormParams", [
      "model" => $this,
      "data" => $data,
      "params" => $params,
    ])["params"];

  }
{% endif %}
}
